@file:JvmName("StringExtensions")

package com.example.mytestdemo.extensions

import java.text.DecimalFormat
import java.util.regex.Pattern


/**
 * Created on 2020/8/18,@author Zen.
 * E-mail:jyzen@foxmail.com
 * This is ground control to Major Tom.
 * Don't panic.
 */


/**
 * 截取指定长度,超过后加上suffix,
 * default suffix = "..."
 */
fun String.elipseEnd(maxLen: Int = 11, suffix: String = "..."): String {
    return if (this.length <= maxLen) this else this.substring(0, maxLen) + suffix
}

/**
 * 截取指定长度,未超过时返回自身
 */
fun String.subOrSelf(maxLen: Int = 11): String {

    return if (this.length <= maxLen) this else this.substring(0, maxLen)
}

/**
 * 如果字符串为空,转成 空字符串 ""
 */
fun String?.safe(defaultValue: String = ""): String {
    return this ?: defaultValue
}

/**
 * 将昵称转化成 第一个字符 + *** :  x***
 */
fun String?.nicknameEllipseEnd(newRule: Boolean): String? {
    if (this.isNullOrEmpty()) return this
    kotlin.runCatching {
        // 校验是否手机号，中间是3个星星，或者4个星星。
        val phoneRegex = "(^1\\d{3}\\*{3}\\d{4}\$)|(^1\\d{2}\\*{4}\\d{4}\$)"
        if (Pattern.matches(phoneRegex, this)) {
            return this
        }
        val nickname = this.trim()
        // 校验是否emoji
        val emojiRegex = "(?:[\\uD83C\\uDF00-\\uD83D\\uDDFF]|[\\uD83E\\uDD00-\\uD83E\\uDDFF]|[\\uD83D\\uDE00-\\uD83D\\uDE4F]|[\\uD83D\\uDE80-\\uD83D\\uDEFF]|[\\u2600-\\u26FF]\\uFE0F?|[\\u2700-\\u27BF]\\uFE0F?|\\u24C2\\uFE0F?|[\\uD83C\\uDDE6-\\uD83C\\uDDFF]{1,2}|[\\uD83C\\uDD70\\uD83C\\uDD71\\uD83C\\uDD7E\\uD83C\\uDD7F\\uD83C\\uDD8E\\uD83C\\uDD91-\\uD83C\\uDD9A]\\uFE0F?|[\\u0023\\u002A\\u0030-\\u0039]\\uFE0F?\\u20E3|[\\u2194-\\u2199\\u21A9-\\u21AA]\\uFE0F?|[\\u2B05-\\u2B07\\u2B1B\\u2B1C\\u2B50\\u2B55]\\uFE0F?|[\\u2934\\u2935]\\uFE0F?|[\\u3030\\u303D]\\uFE0F?|[\\u3297\\u3299]\\uFE0F?|[\\uD83C\\uDE01\\uD83C\\uDE02\\uD83C\\uDE1A\\uD83C\\uDE2F\\uD83C\\uDE32-\\uD83C\\uDE3A\\uD83C\\uDE50\\uD83C\\uDE51]\\uFE0F?|[\\u203C\\u2049]\\uFE0F?|[\\u25AA\\u25AB\\u25B6\\u25C0\\u25FB-\\u25FE]\\uFE0F?|[\\u00A9\\u00AE]\\uFE0F?|[\\u2122\\u2139]\\uFE0F?|\\uD83C\\uDC04\\uFE0F?|\\uD83C\\uDCCF\\uFE0F?|[\\u231A\\u231B\\u2328\\u23CF\\u23E9-\\u23F3\\u23F8-\\u23FA]\\uFE0F?)"
        if (nickname.length > 1 && Pattern.matches(emojiRegex, nickname.substring(0, 2))) {
            return if (newRule && nickname.length > 2) {
                // 判断最后一个是emoji，还是一个普通字符
                if (Pattern.matches(emojiRegex, nickname.substring(nickname.length - 2, nickname.length))) {
                    // 最后一个是表情
                    nickname.substring(0, 2) + "***" + nickname.substring(nickname.length - 2, nickname.length)
                } else {
                    // 最后一个是普通字符
                    nickname.substring(0, 2) + "***" + nickname.substring(nickname.length - 1, nickname.length)
                }
            } else {
                nickname.substring(0, 2) + "***"
            }
        }
        return if (newRule && nickname.length > 1) {
            if (Pattern.matches(emojiRegex, nickname.substring(nickname.length - 2, nickname.length))) {
                // 最后一个是表情
                nickname.substring(0, 1) + "***" + nickname.substring(nickname.length - 2, nickname.length)
            } else {
                // 最后一个是普通字符
                nickname.substring(0, 1) + "***" + nickname.substring(nickname.length - 1, nickname.length)
            }
        } else {
            nickname.substring(0, 1) + "***"
        }
    }
        .exceptionOrNull()
        ?.printStackTrace()
    return this
}

/**
 * 格式化价格
 * 加千分号(,) 无效0可去除
 *
 */
fun String.priceSepara(): String {
    return kotlin.runCatching { DecimalFormat(",###.##").format(this.toDouble()) }.getOrDefault(this)
}

fun String?.toIntOrDefault(defaultValue: Int = 0): Int {
    return kotlin.runCatching { this!!.toInt() }.getOrDefault(defaultValue)
}

fun String?.toIntOrNull(): Int? {
    return kotlin.runCatching { this!!.toInt() }.getOrDefault(null)
}

fun String?.toDoubleOrDefault(defaultValue: Double = 0.0): Double {
    return kotlin.runCatching { this?.toDouble()?: 0.0 }.getOrDefault(defaultValue)
}